package ubiqweb.spring3.dao;

import ubiqweb.spring3.model.Training;

import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("trainingDao")
public class TrainingDaoImpl implements TrainingDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void saveTraining(Training training) {
		sessionFactory.getCurrentSession().saveOrUpdate(training);
	}
	
	@SuppressWarnings("unchecked")
	public List<Training> listTrainings() {		
		return (List<Training>) sessionFactory.getCurrentSession().createCriteria(Training.class).addOrder(Order.desc("trainingDate")).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Training> listTrainings(String trainingId) {		
		return (List<Training>) sessionFactory.getCurrentSession().createCriteria(Training.class).add( Restrictions.eq("trainingId", Integer.parseInt(trainingId))).addOrder( Order.desc("trainingId")).list();
	}
	
	/*@SuppressWarnings("unchecked")
	public List<Training> listTrainings(String trainingId) {		
		return (List<Training>) sessionFactory.getCurrentSession().createQuery("from Training where trainingId = '" + trainingId + "' order by trainingDate desc").list();
	}*/
	


}